package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 分页排序 搜索查询
     * @param page 当前页
     * @param rows 每页的行数
     * @param key  搜索的条件
     * @param sortBy 排序的字段
     * @param desc 是否降序
     * @return
     */
    public PageResult<BrandDTO> queryBrandByPage(Integer page,Integer rows,String key ,String sortBy,Boolean desc){
        //mybatis查询的语句必须写在 这一句的下面才行 中间只要没有mybatis查询就可以
        PageHelper.startPage(page,rows);

        Example example = new Example(Brand.class);
        //不为空
        if(StringUtils.isNoneBlank(key)){
            //这两个字段需要用or  两个字段是或者的关系
            example.createCriteria().orLike("name","%"+key.toUpperCase()+"%")
                    .orEqualTo("letter",key.toUpperCase());
        }

        //排序
        if(StringUtils.isNoneBlank(sortBy)){
            //需要加入  “  ”不然 sql 异常
            String orderByClause = sortBy+" " + (desc ? "DESC" : "ASC");
            example.setOrderByClause(orderByClause);
        }

        //查询
        List<Brand> brands = brandMapper.selectByExample(example);

        //判断是否为空
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        //解析分页结果
        PageInfo<Brand> info = new PageInfo<>(brands);

        //转为BrandDTo
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        return new PageResult<>(info.getTotal(),brandDTOS);
    }

    //新增品牌
    @Transactional
    public void saveBrand(BrandDTO brandDTO,List<Long> cids){
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = brandMapper.insertSelective(brand);
        if(count != 1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //新增
        count =brandMapper.insertCategoryBrand(brand.getId(),cids);
        if(count != cids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    //修改品牌 先删除再添加
    @Transactional
    public void updateBrand(List<Long> cids,BrandDTO brandDTO){
        //修改品牌表的数据
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = brandMapper.updateByPrimaryKey(brand);
        if(count != 1){
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        //删除中间表的数据
        brandMapper.deleteCategoryBrand(brand.getId());

        //插入数据到中间表
        count = brandMapper.insertCategoryBrand(brand.getId(), cids);

        if(count != cids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    //删除品牌
    @Transactional
    public void deleteBrand(Long id){
        //通过id 先删除 中间表的数据 再删除品牌的数据
        brandMapper.deleteCategoryBrand(id);
        int count = brandMapper.deleteByPrimaryKey(id);
        if(count != 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    //通过品牌id 查询品牌信息
    public BrandDTO queryBrandById(Long id){
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }


    public List<BrandDTO> findBrandsById(Long id) {
        List<BrandDTO> list =  brandMapper.findBrandsById(id);
        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }
}
